﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web.webuser
{
    public partial class userinfo : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
                if (user == null) return;
                Hashtable employee = PhoneBook.Group.Employee.get(Commons.StrUtil.FormatValue(user["employeeid"]));
                if (employee == null) return;
                Hashtable department = PhoneBook.Group.Department.get(Commons.StrUtil.FormatValue(employee["departmentid"]));
                txb_employeename.Text = employee["employeename"].ToString();
                txb_age.Text = employee["age"].ToString();
                if (department != null)
                    txb_departmentid.Text = department["departmentname"].ToString();
                txb_mobile.Text = employee["mobile"].ToString();
                txb_phone.Text = employee["phone"].ToString();
                txb_email.Text = employee["email"].ToString();
                txb_shortnum.Text = employee["shortnum"].ToString();
                txb_fax.Text = employee["fax"].ToString();
                txb_duty.Text = employee["duty"].ToString();
                txb_qq.Text = employee["qq"].ToString();
                txb_msn.Text = employee["msn"].ToString();
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
            if (user == null) return;
            Hashtable employee = PhoneBook.Group.Employee.get(Commons.StrUtil.FormatValue(user["employeeid"]));
            if (employee == null) return;

            addLog(employee);

            employee["employeename"] = txb_employeename.Text;
            employee["age"] = txb_age.Text;
            employee["phone"] = txb_phone.Text;
            employee["email"] = txb_email.Text;
            employee["shortnum"] = txb_shortnum.Text;
            employee["fax"] = txb_fax.Text;
            employee["duty"] = txb_duty.Text;
            employee["qq"] = txb_qq.Text;
            employee["msn"] = txb_msn.Text;

            ERMap.ERMapHelper.setmodel("b_employee", "employeeid", employee);
            Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
        }

        //添加日志
        private void addLog(Hashtable employee)
        {
            string content = string.Empty;

            if (employee["employeename"].ToString() != txb_employeename.Text)
                content += string.Format("姓名:{0}->{1};", employee["employeename"], txb_employeename.Text);

            if (employee["age"].ToString() != txb_age.Text)
                content += string.Format("年龄:{0}->{1};", employee["age"], txb_age.Text);

            if (employee["phone"].ToString() != txb_phone.Text)
                content += string.Format("电话:{0}->{1};", employee["phone"], txb_phone.Text);

            if (employee["email"].ToString() != txb_email.Text)
                content += string.Format("邮箱:{0}->{1};", employee["email"], txb_email.Text);

            if (employee["shortnum"].ToString() != txb_shortnum.Text)
                content += string.Format("短号码:{0}->{1};", employee["shortnum"], txb_shortnum.Text);

            if (employee["fax"].ToString() != txb_fax.Text)
                content += string.Format("传真:{0}->{1};", employee["fax"], txb_fax.Text);

            if (employee["duty"].ToString() != txb_duty.Text)
                content += string.Format("职务:{0}->{1};", employee["duty"], txb_duty.Text);

            if (employee["qq"].ToString() != txb_qq.Text)
                content += string.Format("qq:{0}->{1};", employee["qq"], txb_qq.Text);

            if (employee["msn"].ToString() != txb_msn.Text)
                content += string.Format("msn:{0}->{1};", employee["msn"], txb_msn.Text);

            if (!string.IsNullOrEmpty(content))
            {
                content = string.Format("修改了个人信息({0})。{1}", employee["employeename"], content);

                AddLog(content);
            }
        }
    }
}